//
//  TSPresentViewController.m
//  自定义转场动画
//
//  Created by 唐铁山 on 2018/9/11.
//  Copyright © 2018年 唐铁山. All rights reserved.
//

#import "TSPresentViewController.h"

static CGFloat const cubeHalfWidth = 100.0f;

@interface TSPresentViewController ()

@end

@implementation TSPresentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加立方体
    CATransformLayer *cubeLayer = [CATransformLayer layer];

    // Z上
    CATransform3D ct = CATransform3DMakeTranslation(0, 0, cubeHalfWidth);
    [cubeLayer addSublayer:[self faceLayerWithTransform:ct imageName:@"1.jpg"]];

    // X上
    ct = CATransform3DMakeTranslation(cubeHalfWidth, 0, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 0, 1, 0);
    [cubeLayer addSublayer:[self faceLayerWithTransform:ct imageName:@"2.jpg"]];

    // Y上
    ct = CATransform3DMakeTranslation(0, -cubeHalfWidth, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 1, 0, 0);
    [cubeLayer addSublayer:[self faceLayerWithTransform:ct imageName:@"3.jpg"]];

    // Y下
    ct = CATransform3DMakeTranslation(0, cubeHalfWidth, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 1, 0, 0);
    [cubeLayer addSublayer:[self faceLayerWithTransform:ct imageName:@"6.jpg"]];

    // X下
    ct = CATransform3DMakeTranslation(-cubeHalfWidth, 0, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 0, 1, 0);
    [cubeLayer addSublayer:[self faceLayerWithTransform:ct imageName:@"5.png"]];

    // Z下
    ct = CATransform3DMakeTranslation(0, 0, -cubeHalfWidth);
    [cubeLayer addSublayer:[self faceLayerWithTransform:ct imageName:@"4.jpeg"]];
 
    cubeLayer.position = self.view.center;
    cubeLayer.transform = CATransform3DMakeRotation(M_PI / 3.0f, 1.0, 1.0, 0);
    [self.imageView.layer addSublayer:cubeLayer];
    
    [self.view bringSubviewToFront:self.dismissButton];
    self.imageView.clipsToBounds = YES;
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}


- (CALayer *)faceLayerWithTransform:(CATransform3D)ct imageName:(NSString *)imageName {
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(0, 0, cubeHalfWidth * 2.0f, cubeHalfWidth * 2.0f);
    layer.contents = (__bridge id)[UIImage imageNamed:imageName].CGImage;
    
    
    layer.transform = ct;
    return layer;
}

- (IBAction)dismissButtonClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
